package com.kobeliu.sharding_demo.strategy;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

// 分库
public class CustomDBPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> datasourceNames, PreciseShardingValue<Long> preciseShardingValue) {

        for(String datasourceName : datasourceNames){
            // 0,1,2
            String value = preciseShardingValue.getValue() % datasourceNames.size() + "";
            if(datasourceName.endsWith(value)){
                return datasourceName;
            }

        }

        return null;
    }
}
